package Server;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class GetXml
 */
public class GetXml extends HttpServlet {
	private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
	private Date cal = null;	
	private static final long serialVersionUID = 1L;
	private PackageHandler pack = null;
	private Map<String,String> stable = null;
	private Map<String,String> unstable = null;
	private Map<String,String> testing = null;
	private Map<String,String> experimental = null;
//	private Map<String,String> backports = null;
	
	private Map<String,Boolean> Keys = new TreeMap<String,Boolean>();

       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetXml() {
        super();
    }
    public void init() {
    	System.err.println("init()");
    	pack = new PackageHandler();
    	Iterator<String> i=null;
    	
    	// stable
    	System.err.println("-stable");
    	stable = pack.getPackages("stable");
    	i = stable.keySet().iterator();
    	while ( i.hasNext() )
    		Keys.put(i.next(), true);
    	// unstable
    	System.err.println("-unstable");
    	unstable = pack.getPackages("unstable");
    	i = unstable.keySet().iterator();
    	while ( i.hasNext() )
    		Keys.put(i.next(), true);
    	// testing
    	System.err.println("-testing");
    	testing = pack.getPackages("testing");
    	i = testing.keySet().iterator();
    	while ( i.hasNext() )
    		Keys.put(i.next(), true);
    	// experimental
    	System.err.println("-experimental");
    	experimental = pack.getPackages("experimental");
    	i = experimental.keySet().iterator();
    	while ( i.hasNext() )
    		Keys.put(i.next(), true);
    	// backports
    	
    	
/*    	
 		// example update
 		Map<String,String> temp =  pack.getPackages("stable");
    	synchronized(stable) {
    		stable=temp;
    	}HttpServlet
*/
    	
/*    	
    	backports = pack.getPackages("backports");
    	i = backports.keySet().iterator();
    	while ( i.hasNext() )
    		Keys.put(i.next(), true);
*/  	
    	System.err.println("init() done");
    	cal = Calendar.getInstance().getTime();
    }
    public void destroy() {
    	System.err.println("destroy()");
    	pack = null;
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletOutputStream out = response.getOutputStream();
		// String pos=(String)request.getParameter("order");
		response.setContentType("text/xml");
		out.println("<?xml-stylesheet type=\"text/xsl\" href=\"layout.xsl\"?>");
		out.println("<Packages updated=\""+new SimpleDateFormat(DATE_FORMAT_NOW).format(cal)+"\" title=\"Debian Packages\" >");
		for ( Entry<String, Boolean>  c : Keys.entrySet() ) {
			
			// if 
			out.println("\t<Package>");
			out.println("\t\t<Name>"+c.getKey()+"</Name>");
			
			if ( stable.containsKey(c.getKey()))
				out.println("\t\t<Stable>"+stable.get(c.getKey())+"</Stable>");
			else
				out.println("\t\t<Stable></Stable>");
			
			if ( unstable.containsKey(c.getKey()))
				out.println("\t\t<Unstable>"+unstable.get(c.getKey())+"</Unstable>");
			else
				out.println("\t\t<Unstable></Unstable>");
			
			if ( testing.containsKey(c.getKey()))
				out.println("\t\t<Testing>"+testing.get(c.getKey())+"</Testing>");
			else
				out.println("\t\t<Testing></Testing>");	
			
			if ( experimental.containsKey(c.getKey()))
				out.println("\t\t<Experimental>"+experimental.get(c.getKey())+"</Experimental>");
			else
				out.println("\t\t<Experimental></Experimental>");
			
/*			if ( backports.containsKey(c.getKey()))
				out.println("\t\t<Backports>"+backports.get(c.getKey())+"</Backports>");
			else
				out.println("\t\t<Backports></Backports>");				
*/			
			out.println("\t</Package>");
		}
		out.println("</Packages>");
//		out.println( stable.toString() );
	}

}
